package com.qf.java2103.controller;


import com.qf.java2103.pojo.Equipment;
import com.qf.java2103.resultVO.ResultVO;
import com.qf.java2103.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @PostMapping("/add")
    public ResultVO add(@RequestBody Equipment equipment){
        if (Objects.isNull(equipment)){
            return ResultVO.error("参数错误");
        }
        equipmentService.add(equipment);
        return ResultVO.ok("添加成功",null);
    }

    @GetMapping("/del")
    private ResultVO deleteById(Long id){
        equipmentService.deleteById(id);
        return ResultVO.ok("删除成功",null);
    }

    @GetMapping("/editUI/{id}")
    public ResultVO editUI(@PathVariable Long id){
        Equipment equipment = equipmentService.selectById(id);
        return ResultVO.ok("查询成功",equipment);
    }

    @PostMapping("/edit")
    public ResultVO edit(@RequestBody Equipment equipment){
        if (Objects.isNull(equipment)){
            return ResultVO.error("参数错误");
        }
        equipmentService.edit(equipment);
        return ResultVO.error("修改成功",null);
    }

    @GetMapping("/selectAll/{id}")
    public ResultVO selectAll(@PathVariable(name = "id") Long buildingEquipment){
        List<Equipment> list = equipmentService.getList(buildingEquipment);
        return ResultVO.ok("数据:",list);
    }

    @RequestMapping("/select/{name}")
    public ResultVO selectByName(@PathVariable String name){
        System.out.println(name);
        try {
            List<Equipment> equipment = equipmentService.selectByName(name);
            return ResultVO.ok("数据",equipment);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error("查询失败",e.getMessage());
        }
    }

}
